package com.hq.vieorecyclerview.video.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.hq.vieorecyclerview.R;
import com.hq.vieorecyclerview.video.Adapter.HomeAdapter;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页导航页面
 */
public class HomePageFragment extends Fragment implements BottomNavigationView.OnNavigationItemSelectedListener, ViewPager.OnPageChangeListener {
    private ViewPager viewPager=null;
    private BottomNavigationView bott=null;
    private List<Fragment> fragments=null;
    private HomeAdapter adapter =null;
    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.fragments = new ArrayList<>();
        this.adapter = new HomeAdapter(getActivity().getSupportFragmentManager(), this.fragments);
        this.fragments.add(new HomeFragment());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //加载布局
        View v = inflater.inflate(R.layout.fragment_home_page, container, false);
        this.viewPager = v.findViewById(R.id.home_pager);
        this.viewPager.setAdapter(this.adapter);
        //滑动注册
        this.viewPager.addOnPageChangeListener(this);
        //按钮点击事件
        this.bott = v.findViewById(R.id.tabs);
        this.bott.setOnNavigationItemSelectedListener(this);


        return v;
    }

    @Override
    public void onStart() {
        super.onStart();

        if (getActivity() != null) {
            FragmentManager fragmentManager = getFragmentManager();
            if (fragmentManager != null) {
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.detach(this).attach(this).commit();
            }
        }





        this.viewPager.setCurrentItem(0);

        Log.d("onBindViewHolder","数量= HomePageFragment "+viewPager.getCurrentItem());
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull @NotNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_home:
                this.viewPager.setCurrentItem(0);
                break;
            case R.id.menu_video:
                this.viewPager.setCurrentItem(1);
                break;
            case R.id.menu_lisi:
                this.viewPager.setCurrentItem(2);
                break;
            case R.id.menu_gern:
                this.viewPager.setCurrentItem(3);
                break;
        }
        return true;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

        //滑动抬起
        switch (position) {
            case 0:
                this.bott.setSelectedItemId(R.id.menu_home);
                break;
            case 1:
                this.bott.setSelectedItemId(R.id.menu_video);
                break;
            case 2:
                this.bott.setSelectedItemId(R.id.menu_lisi);
                break;
            case 3:
                this.bott.setSelectedItemId(R.id.menu_gern);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {


    }
}
